package com.settlement.system.converter;

import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.settlement.system.common.enums.WpTypeEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @author author
 */
public class WpVoucherSummaryConvert extends StringStringConverter {

    @Override
    public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {

        if(!StringUtils.isEmpty(value)){
            WpTypeEnum.VOUCHER_SUMMARY[] voucherSummaries = WpTypeEnum.VOUCHER_SUMMARY.values();
            for(int index = 0;index < voucherSummaries.length;index++){
                if(voucherSummaries[index].getNo().equals(value)){
                    return new WriteCellData(voucherSummaries[index].getName());
                }
            }
        }
        return new WriteCellData(value);
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String value = super.convertToJavaData(cellData, contentProperty, globalConfiguration);
        if(!StringUtils.isEmpty(value)){
            WpTypeEnum.VOUCHER_SUMMARY[] voucherSummaries = WpTypeEnum.VOUCHER_SUMMARY.values();
            for(int index = 0;index < voucherSummaries.length;index++){
                if(voucherSummaries[index].getName().equals(value)){
                    return voucherSummaries[index].getNo();
                }
            }
        }
        return null;
    }
}
